<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class GoodslistController extends Controller
{
    public function getIndex(Request $request,$effectid='')
    {
        $id = array();
        foreach($_GET as $k=>$v){
            $id[$k] = $v;
        }
    	$res = self::getCates(0);
    	$brand = array();
        $brandid = array();
    	foreach($res as $k => $v){
    		foreach($v->sub as $ks => $vs){
    			if(!in_array($vs->name,$brand)){
                    $brandid[] = $vs->id;
    				$brand[] = $vs->name;
    			}
    		}
    	}
        $goodsort = array();

    	$effect = DB::table('effect')->get();
        if(!empty($id['brandid'])){
            $goods = DB::table('seckillglobal')->where('brand',$id['brandid'])->get();
            return view('home.goodslist.index',['brand'=>$brand,'brandid'=>$brandid,'effect'=>$effect,'goods'=>$goods,'goodsort'=>$goodsort]);
        }

        if(!empty($id['pricemax'])){
            $goods = DB::table('seckillglobal')->where('price','>',$id['pricemin'])->where('price','<',$id['pricemax'])->get();
            return view('home.goodslist.index',['brand'=>$brand,'brandid'=>$brandid,'effect'=>$effect,'goods'=>$goods,'goodsort'=>$goodsort]);
        }

        if(!empty($id['cateid'])){
            $categid = DB::table('goodsort')->where('id',$id['cateid'])->value('gid');
            $goodsort = self::getCatesByPid($categid);

            $goods = DB::table('seckillglobal')->where('cate',$id['cateid'])->get();
            return view('home.goodslist.index',['brand'=>$brand,'brandid'=>$brandid,'effect'=>$effect,'goods'=>$goods,'goodsort'=>$goodsort]);
        }else{

            $goods = array();
            return view('home.goodslist.index',['brand'=>$brand,'brandid'=>$brandid,'effect'=>$effect,'goods'=>$goods,'goodsort'=>$goodsort]);
        }
    }

    public static function getCatesByPid($gid){
        $res = DB::table('goodsort')->where('gid',$gid)->get();
        $data = [];
        //遍历
        foreach ($res as $k=>$v){
            $v->sub = self::getCatesByPid($v->id);
            $data[] = $v;
        }
        return $data;
    }
    public static function getCates($gid){
        $res = DB::table('brand')->where('gid',$gid)->get();
        $data = [];
        //遍历
        foreach ($res as $k=>$v){
            $v->sub = self::getCates($v->id);
            $data[] = $v;
        }
        return $data;
    }
}
